<?php


namespace app\admin\controller;


use app\model\ModelUser;
use app\model\ModelUserAccountRechange;
use app\service\PaymentService;
use app\service\TaskService;
use app\service\UserAccountService;
use easyadmin\app\columns\lists\ListImage;
use easyadmin\app\columns\lists\ListText;
use easyadmin\app\libs\ListField;
use easyadmin\app\libs\Page;
use easyadmin\app\libs\PageList;
use easyadmin\app\libs\Pagination;
use think\db\Query;
use think\Model;
use think\response\Json;

/**
 * 用户充值审核
 * Class ReviewUserRecharge
 * @package app\admin\controller
 */
class ReviewUserRecharge extends  Admin
{
    protected $tableName = 'user_account_recharge';
    protected $pageName = '用户充值审核';
    protected $disabledAction = ['delete' ,'add', 'show', 'edit'];


    protected function configListJoin(Page $page, Query $query, string $alias)
    {
        $query->join('user', 'user.id='. $alias .'.user_id');
        $query->join('receivable', 'receivable.id='. $alias .'.receivable_id');
    }
    protected function configListWhere(Page $page, Query $query, $alias)
    {
        $query->where([
            'user_account_recharge.status' => 1 ,//未审核状态 1:未审核 2:已审核通过 3:审核不通过
            'user_account_recharge.payment_status' => 2 //订单已支付 1:待付款 2:已付款 3:取消订单
        ]);
    }

    protected function configListPagination(Pagination $pagination)
    {
        $pagination->setPageSize(20);
    }

    protected function configListField(ListField $list)
    {
        $list
            ->addField('id', 'ID', ListText::class)
            ->addField('user_id', '用户ID', ListText::class)
            ->addField('recharge_number', '充值流水号', ListText::class)
            ->addField('receivable.card_number', '收款账户', ListText::class)
            ->addField('user.real_name', '用户名', ListText::class)
            ->addField('recharge_profit', '充值金额', ListText::class)
            ->addField('recharge_type', '充值类型', ListText::class)
            ->addField('before_balance', '充值前金额', ListText::class)
            ->addField('vip_lv', '是否充值VIP', ListText::class)
            ->addField('ping_zheng_pic_url', '用户支付凭证', ListImage::class)
            ->addField('remark', '用户支付备注', ListText::class)
            ;
    }

    protected function configList(PageList $page)
    {
        $page
            ->addAction('审核通过', 'reviewRecharge?type=2',[
                'confirm' => '确定审核通过',
            ])
            ->addAction('审核驳回', 'reviewRecharge?type=3',[
                'class' => 'layui-btn layui-btn-warm',
                'confirm' => '确定审核驳回',
            ])
            ;
        $page
            ->setOrderBy(["id"=>"desc"])
            ->setTemplate("list:list")
        ;
        $this->assign("review","review");
        $this->assign("typeY",2);
        $this->assign("typeN",3);
    }

    /**
     * 审核用户充值订单
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function reviewRecharge(): Json
    {
        $input = input();
        $id = trim($input['id']);
        $type = trim($input['type']);
        $loginUserId = $this->getLoginUserId();
        $rechargeModel = new ModelUserAccountRechange();
        $userId = $rechargeModel->field('user_id')->find($id)['user_id'];
        if(empty($userId)) return $this->error('查不到用户id');
        $res = UserAccountService::instance()->checkUerRecharge($userId, $id, $type, $loginUserId, $this->tableName);
        if($res['status']) {
            return $this->success($res);
        }else{
            return $this->error($res);
        }
    }

    public function reviewBatch() {
        $input = input();
        $id = trim($input['ids']);
        $type = trim($input['type']);
        $loginUserId = $this->getLoginUserId();
        $rechargeModel = new ModelUserAccountRechange();
        $userId = $rechargeModel->field('user_id')->find($id)['user_id'];
        if(empty($userId)) return $this->error('查不到用户id');
        $res = UserAccountService::instance()->checkUerRecharge($userId, $id, $type, $loginUserId, $this->tableName);
        if($res['status']) {
            return $this->success($res);
        }else{
            return $this->error($res);
        }
    }
}